<template>
  <div id="header">
    <a-row :wrap="false">
      <a-col flex="250px">
        <router-link to="/">
          <div class="title-bar">
            <img src="../assets/logo.png" alt="logo" class="logo" />
            <div class="title">DS新能源制氢小助手</div>
          </div>
        </router-link>
      </a-col>
      <a-col flex="auto">
        <a-menu
          v-model:selectedKeys="current"
          mode="horizontal"
          :items="items"
        />
      </a-col>
      <a-col flex="120px">
        <div class="user-login-status">
          <div v-if="authStore.isAuthenticated" class="user-login-status">
            您好， {{ authStore.user.username ?? "User" }}
            <a-button type="primary" danger @click="handleLogout" class="logout"
              >退出
            </a-button>
          </div>
          <div v-else>
            <a-button type="primary" href="/user/login" class="login">
              登陆
            </a-button>
          </div>
        </div>
      </a-col>
    </a-row>
  </div>
</template>
<script setup>
import { h, ref } from "vue";
import { HomeOutlined } from "@ant-design/icons-vue";
import router from "@/router/index.js";

const current = ref([]);
router.afterEach((to, from, next) => {
  current.value = [to.path];
});

const items = ref([
  {
    key: "/",
    icon: () => h(HomeOutlined),
    label: "主页",
    title: "主页",
  },
]);

import { useAuthStore } from "@/stores/LoginUserStore.js";

const authStore = useAuthStore();

const handleLogout = () => {
  authStore.logout();
};
</script>

<style scoped>
#header {
  flex-direction: row;
}

#header .title-bar {
  display: flex;
  align-items: center;
}

.title {
  color: black;
  align-items: center;
  font-size: 18px;
  margin-left: 20px;
}

.logo {
  height: 48px;
}
.user-login-status{
  display: flex;
  align-items: center;
}
.logout {
  justify-content: right;
}
.user-login-status{
  display: flex;
  align-items: center;
}
</style>
